package com.ruoyi.common.utils;

import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.tea.TeaException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.ExecutionException;

/**
 * 阿里云短信
 * @Author Alan Zhang
 * @Date 2025/6/17 10:15
 */
public class AliSendMessageUtil {
    private static final Logger log = LoggerFactory.getLogger(AliSendMessageUtil.class);
    private static final String ALIBABA_CLOUD_ACCESS_KEY_ID = "LTAI5t9Lq7sPSuV6dohR46Zj";//LTAI5tLezJJaq1z78214vgK9   //RAM用户
    private static final String ALIBABA_CLOUD_SECURITY_TOKEN = "O17J7aYZPZSGOxMb9JcrwZdR7V59FT";//TBX9A5StXwHDKuxCYKXISzPX5e1yIq
    private static final String JINTONG_ZH = "进通", JINTONG2_ZH="进通新能源", JINTONG_EN = "JinTong";

    public static com.aliyun.dysmsapi20170525.Client createClient() throws Exception {
        // 工程代码建议使用更安全的无AK方式，凭据配置方式请参见：https://help.aliyun.com/document_detail/378657.html。
        com.aliyun.credentials.Client credential = new com.aliyun.credentials.Client();
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                .setCredential(credential);
        config.setAccessKeyId(ALIBABA_CLOUD_ACCESS_KEY_ID);
        config.setAccessKeySecret(ALIBABA_CLOUD_SECURITY_TOKEN);
        // Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new com.aliyun.dysmsapi20170525.Client(config);
    }

    //登录  短信码
    public static String signInSendMessage (String phoneNumber, String code) throws Exception {
        com.aliyun.dysmsapi20170525.Client client = createClient();
        com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
                .setSignName(JINTONG2_ZH)
                .setTemplateCode("SMS_320996479")
                .setTemplateParam("{\"code\":\""+code+"\"}")
                .setPhoneNumbers(phoneNumber);
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        try {
            // 复制代码运行请自行打印 API 的返回值
            SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, runtime);
            String resMsg = com.aliyun.teautil.Common.toJSONString(sendSmsResponse.getBody());

            log.info("ali 国内短信："+com.aliyun.teautil.Common.toJSONString(sendSmsResponse));
            return resMsg;
        } catch (TeaException error) {
            // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
            // 错误 message
            String resMsg = error.getMessage();
            log.info(resMsg);
            // 诊断地址
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
            return resMsg;
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
            // 错误 message
            String resMsg = error.getMessage();
            log.info(resMsg);
            // 诊断地址
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
            return resMsg;
        }
    }

    //注册 短信码
    public static String signUpSendMessage (String phoneNumber, String code) throws Exception {
        com.aliyun.dysmsapi20170525.Client client = createClient();
        com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
                .setSignName(JINTONG2_ZH)
                .setTemplateCode("SMS_489085208")
                .setTemplateParam("{\"code\":\""+code+"\"}")
                .setPhoneNumbers(phoneNumber);
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        try {
            // 复制代码运行请自行打印 API 的返回值
            SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, runtime);
            String resMsg = com.aliyun.teautil.Common.toJSONString(sendSmsResponse.getBody());

            log.info("ali 国内短信："+com.aliyun.teautil.Common.toJSONString(sendSmsResponse));
            return resMsg;
        } catch (TeaException error) {
            // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
            // 错误 message
            String resMsg = error.getMessage();
            log.info(resMsg);
            // 诊断地址
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
            return resMsg;
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
            // 错误 message
            String resMsg = error.getMessage();
            log.info(resMsg);
            // 诊断地址
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
            return resMsg;
        }
    }

    /**
     *  登录 境外 短信码
     * @param phoneNumber
     * @param code
     * @return
     * @throws ExecutionException
     * @throws InterruptedException
     */
    public static String signInSendMessageToGlobe(String phoneNumber, String code) throws Exception {
        String message = String.format("[%s] Your verification code is: %s, please do not disclose it to others!", JINTONG_EN, code);
        // Parameter settings for API request
        com.aliyun.dysmsapi20170525.Client client = createClient();
        com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
                .setSignName(JINTONG_EN)
                .setTemplateCode("SMS_489085208")
                .setTemplateParam("{\"code\":\""+code+"\"}")
                .setPhoneNumbers(phoneNumber);
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        try {
            // 复制代码运行请自行打印 API 的返回值
            SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, runtime);
            String resMsg = com.aliyun.teautil.Common.toJSONString(sendSmsResponse.getBody());

            log.info("ali 国外短信："+com.aliyun.teautil.Common.toJSONString(sendSmsResponse));
            return resMsg;
        } catch (TeaException error) {
            // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
            // 错误 message
            String resMsg = error.getMessage();
            log.info(resMsg);
            // 诊断地址
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
            return resMsg;
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
            // 错误 message
            String resMsg = error.getMessage();
            log.info(resMsg);
            // 诊断地址
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
            return resMsg;
        }
    }

    /**
     * 注册 境外 短信码
     * @param phoneNumber
     * @param code
     * @return
     * @throws ExecutionException
     * @throws InterruptedException
     */
    public static String signUpSendMessageToGlobe(String phoneNumber, String code) throws Exception {
       String message = String.format("[%s] You are applying for mobile registration, the verification code is: %s, valid for 5 minutes!", JINTONG_EN, code);
        // Parameter settings for API request
        com.aliyun.dysmsapi20170525.Client client = createClient();
        com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
                .setSignName(JINTONG_EN)
                .setTemplateCode("SMS_489085208")
                .setTemplateParam("{\"code\":\""+code+"\"}")
                .setPhoneNumbers(phoneNumber);
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        try {
            // 复制代码运行请自行打印 API 的返回值
            SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, runtime);
            String resMsg = com.aliyun.teautil.Common.toJSONString(sendSmsResponse.getBody());

            log.info("ali 国外短信："+com.aliyun.teautil.Common.toJSONString(sendSmsResponse));
            return resMsg;
        } catch (TeaException error) {
            // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
            // 错误 message
            String resMsg = error.getMessage();
            log.info(resMsg);
            // 诊断地址
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
            return resMsg;
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
            // 错误 message
            String resMsg = error.getMessage();
            log.info(resMsg);
            // 诊断地址
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
            return resMsg;
        }
    }


    /**
     * 供应商 采购短信
     * @return
     */
    public static String supplierPurchaseSendMessage(String supplierName, String orderNo, String phoneNumber) throws Exception {
        com.aliyun.dysmsapi20170525.Client client = createClient();
        com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
                .setSignName(JINTONG2_ZH)
                .setTemplateCode("SMS_490775200")
                .setTemplateParam("{\"name\":\""+supplierName+"\",\"order\":\""+orderNo+"\"}")
                .setPhoneNumbers(phoneNumber);
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        try {
            // 复制代码运行请自行打印 API 的返回值
            SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, runtime);
            String resMsg = com.aliyun.teautil.Common.toJSONString(sendSmsResponse.getBody());

            log.info("ali 国内短信："+com.aliyun.teautil.Common.toJSONString(sendSmsResponse));
            return resMsg;
        } catch (TeaException error) {
            // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
            // 错误 message
            String resMsg = error.getMessage();
            log.info(resMsg);
            // 诊断地址
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
            return resMsg;
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
            // 错误 message
            String resMsg = error.getMessage();
            log.info(resMsg);
            // 诊断地址
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
            return resMsg;
        }
    }

}
